package com.gscitysfy.cus.drain.modules.reserveplan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gscitysfy.cus.drain.modules.reserveplan.entity.DrainReservePlanControlInfo;
import com.gscitysfy.cus.drain.modules.reserveplan.entity.DrainReservePlanInfo;
import com.gscitysfy.cus.drain.modules.reserveplan.mapper.DrainReservePlanControlInfoMapper;
import com.gscitysfy.cus.drain.modules.reserveplan.mapper.DrainReservePlanInfoMapper;
import com.gscitysfy.cus.drain.modules.reserveplan.service.IDrainReservePlanControlInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 预案管理-指挥体系信息表 服务实现类
 * </p>
 *
 * @author shenglin
 * @since 2022-11-15 17:47:50
 */
@Service
public class DrainReservePlanControlInfoServiceImpl extends ServiceImpl<DrainReservePlanControlInfoMapper, DrainReservePlanControlInfo> implements IDrainReservePlanControlInfoService {

    @Autowired
    private DrainReservePlanControlInfoMapper drainReservePlanControlInfoMapper;

    @Override
    public void deletByInfoId(List<String> infoIds) {
        LambdaQueryWrapper<DrainReservePlanControlInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(DrainReservePlanControlInfo::getInfoId,infoIds);
        List<DrainReservePlanControlInfo> drainReservePlanControlInfos = drainReservePlanControlInfoMapper.selectList(queryWrapper);
        for (DrainReservePlanControlInfo drainReservePlanControlInfo : drainReservePlanControlInfos) {
            drainReservePlanControlInfo.setCmUsable(true);
        }
        this.updateBatchById(drainReservePlanControlInfos);
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public int saveBathReservePlanControlInfo(List<DrainReservePlanControlInfo> drainReservePlanControlInfo) {
        if(!drainReservePlanControlInfo.isEmpty()){
            //删除原有的指挥体系信息
            LambdaQueryWrapper<DrainReservePlanControlInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DrainReservePlanControlInfo::getInfoId,drainReservePlanControlInfo.get(0).getInfoId());
            List<DrainReservePlanControlInfo> old = drainReservePlanControlInfoMapper.selectList(queryWrapper);
            for (DrainReservePlanControlInfo info : old) {
                info.setCmUsable(true);
            }
            this.updateBatchById(old);
            int i=0;
            for (DrainReservePlanControlInfo reservePlanControlInfo : drainReservePlanControlInfo) {
                //预案指挥体系的预案id不能为空
                if(reservePlanControlInfo!=null&& reservePlanControlInfo.getInfoId()!=null){
                    i+=drainReservePlanControlInfoMapper.insert(reservePlanControlInfo);
                }
            }
            return i;
        }
        return 0;
    }

    @Override
    public List<DrainReservePlanControlInfo> getReservePlanControlInfoList(String infoId) {
        LambdaQueryWrapper<DrainReservePlanControlInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(DrainReservePlanControlInfo::getInfoId,infoId);
        queryWrapper.eq(DrainReservePlanControlInfo::getCmUsable,false);
        return drainReservePlanControlInfoMapper.selectList(queryWrapper);
    }
}
